package com.example.shopp_2301.createorder

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.base.entity.GetPreOderEntity
import com.example.base.entity.ParamCreateOrder
import com.example.base.mvvm.BaseActivity
import com.example.shopp_2301.R
import com.example.shopp_2301.adapter.GerPreAdapter
import com.example.shopp_2301.databinding.ActivityCreateOrderBinding
@Route(path = "/app/CreateOrderActivity")
class CreateOrderActivity : BaseActivity<ActivityCreateOrderBinding,CreateOrderViewModel>() {
    @Autowired
    @JvmField
    var preOrderNo = ""

    lateinit var gerPreAdapter: GerPreAdapter
    override fun initView() {
        ARouter.getInstance().inject(this)
        viewModel.getPreOrder(preOrderNo)
        gerPreAdapter = GerPreAdapter()
        binding.rvGoods.adapter = gerPreAdapter
        binding.rvGoods.layoutManager = LinearLayoutManager(this)

        binding.create.setOnClickListener {
            val paramCreateOrder = ParamCreateOrder()
            paramCreateOrder.addressId = 326
            paramCreateOrder.preOrderNo = preOrderNo
            viewModel.createOrder(paramCreateOrder)
        }

    }

    override fun initData() {
        viewModel.getSuccess.observe(this){
            if(it.code==200){
                //名称和手机号
                binding.tvName.text = "${it.data.orderInfoVo.realName}    ${it.data.orderInfoVo.phone}"
                val orderInfoVo = it.data.orderInfoVo
                //地址拼接
                binding.tvAddress.text = "${orderInfoVo.province}${orderInfoVo.city}${orderInfoVo.district}${orderInfoVo.detail}"
                binding.tvCount.text = "共${it.data.orderInfoVo.orderProNum}种商品"
                binding.tvTotal.text = "合计${it.data.orderInfoVo.payFee}元"

                gerPreAdapter.data.clear()
                gerPreAdapter.data.addAll(it.data.orderInfoVo.orderDetailList)
                gerPreAdapter.notifyDataSetChanged()

            }
        }
        viewModel.createSuccess.observe(this){
            if(it.code==200){
                ARouter.getInstance().build("/app/PayActivity").withString("orderNo",it.data.orderNo).navigation()
            }
        }
    }

    override fun getViewModel(): Class<CreateOrderViewModel> {
        return CreateOrderViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.activity_create_order
    }

}